package qnb.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qnb.dao.StudentDao;
import qnb.dao.TeacherDao;
import qnb.entity.Student;
import qnb.entity.Teacher;
import qnb.service.UserService;

import javax.servlet.http.HttpSession;

/**
 * @author huangxin
 * @date 2019/6/8-22:43
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private TeacherDao teacherDao;

    /**
     * 更新密码
     * @param session
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @Override
    public int updatePassword(HttpSession session, String oldPassword, String newPassword) {
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser instanceof  Teacher){
            Teacher user = (Teacher) loginUser;
            Teacher teacher = teacherDao.selectOne(user.getTeacherId());
            //比较
            if (teacher.getTeacherPassword().equals(oldPassword)){
                //更新
                return teacherDao.updatePassword(teacher.getTeacherId(),newPassword);
            }

        }else if(loginUser instanceof  Student){
            Student user = (Student) loginUser;
            Student student = studentDao.selectOne(user.getStudentId());
            //比较
            if (student.getStudentPassword().equals(oldPassword)){
                student.setStudentPassword(newPassword);
                //更新
                return studentDao.updateByPrimaryKeySelective(student);
            }
        }
        return -1;
    }
}
